package com.xrit.service;

import com.xrit.pojo.UserInfo;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.util.ArrayList;
import java.util.List;

/**
 * 类名称: UserService
 * 类描述:TODO
 * 创建人: czb
 * 创建时间:2022/7/5 下午 02:16
 * Version 1.0.0
 */
public class UserService implements UserDetailsService {
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserInfo userInfo = byUser(username);
        List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
        grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        grantedAuthorities.add(new SimpleGrantedAuthority("add"));
        return new User(username,userInfo.getPassword(),grantedAuthorities);
    }
    public UserInfo byUser(String username){
        if("chen".equals(username)){
            UserInfo chen = new UserInfo("chen", "$2a$10$14zxUC7raRl7N4RVaIA6NOi0xd5w5RCRhEWVjrmamwjEb/HzGOVxi");
            return chen ;
        }
return null;
    }
    public static void main(String[] args) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
//        String encode = encoder.encode("123");
//        System.out.println(encode);
        System.out.println(encoder.matches("123", "$2a$10$5zqYqaoDMM3So7jlriwbWOo6LFq0F1wXTqcnGKIReSAV1yuvwIzl."));
    }
}
